﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Configuration;
using System.Web;
using Top.Api;
using Top.Api.Domain;
using Top.Api.Request;
using Top.Api.Response;
using DBControl;
using System.Data;

namespace KSGJ.AjaxCommand
{
    /// <summary>
    /// chanelpromotion 的摘要说明
    /// </summary>
    public class chanelpromotion : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        private static ITopClient client = new DefaultTopClient(WebConfigurationManager.AppSettings["AppUrl"].ToString(), WebConfigurationManager.AppSettings["AppKey"].ToString(), WebConfigurationManager.AppSettings["AppSecret"].ToString());

        private string errMsg = "";

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string deleteid = context.Request.Form["Did"];
            SqlDbHelper sdh = new SqlDbHelper();
            //string pid = context.Request.Form["pid"];
            string getdeletepro = "SELECT Pid FROM Promotion WHERE ParentID = '" + deleteid + "'";
            DataTable dt = sdh.ExecuteDataTable(getdeletepro);
            if (dt.Rows.Count > 0)
            {
                MarketingPromotionDeleteRequest mpdreq = new MarketingPromotionDeleteRequest();
                int errcount = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    mpdreq.PromotionId = long.Parse(dt.Rows[i]["Pid"].ToString());
                    MarketingPromotionDeleteResponse mpdrep = client.Execute(mpdreq, context.Session["sessionKey"].ToString());
                    if (mpdrep.IsError)
                    {
                        errMsg = mpdrep.SubErrMsg;
                        errcount += 1;
                    }
                    else
                    {
                        string deletepro = "DELETE Promotion WHERE Pid = '" + dt.Rows[i]["Pid"].ToString() + "'";
                        sdh.ExecuteNonQuery(deletepro);
                    }
                }
                if (errcount == 0)
                {
                    string deletepactive = "DELETE Promotionactive WHERE PromotionactiveID = '" + deleteid + "'";
                    sdh.ExecuteNonQuery(deletepactive);
                }
            }
            else
            {
                string deletepactive = "DELETE Promotionactive WHERE PromotionactiveID = '" + deleteid + "'";
                sdh.ExecuteNonQuery(deletepactive);
            }
            
            //MarketingPromotionsGetRequest promotionget = new MarketingPromotionsGetRequest();
            //promotionget.Fields = "promotion_id,start_date,end_date,status,tag_id,discount_value,discount_type";
            //promotionget.NumIid = goodid;
            //promotionget.Status = "ACTIVE";
            //promotionget.TagId = 1;
            //MarketingPromotionsGetResponse promotionresp = client.Execute(promotionget, context.Session["sessionKey"].ToString());
            //if (!promotionresp.IsError)
            //{
            //    MarketingPromotionDeleteRequest mpdreq = new MarketingPromotionDeleteRequest();
            //    foreach (var item in promotionresp.Promotions)
            //    {
            //        mpdreq.PromotionId = item.PromotionId;
            //        MarketingPromotionDeleteResponse mpdrep = client.Execute(mpdreq, context.Session["sessionKey"].ToString());
            //        if (mpdrep.IsError)
            //        {
            //            errMsg = mpdrep.SubErrMsg;
            //        }
            //    }
            //}

            if (errMsg != "")
            {
                context.Response.Write("取消活动失败,原因:" + errMsg);
                context.Response.End();
            }
            else
            {
                context.Response.Write("ok");
                context.Response.End();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}